import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/common_model/list_handbook_model.dart';
import 'package:yysk_app/model/user_prop_model/list_by_propId_history_model.dart';
import 'package:yysk_app/net/api/api_url.dart';
import 'package:yysk_app/net/api/common_api.dart';
import 'package:yysk_app/net/http.dart';
import 'package:yysk_app/pages/main/home/handbook_detail_page.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/widget/custom_app_bar2.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';
import 'package:yysk_app/widget/lazy_network_image.dart';
import 'package:yysk_app/widget/refresh_and_scroll_widget.dart';

class NewcomerHandbookPage extends StatefulWidget {
  static const path = '/NewcomerHandbookPage';
  const NewcomerHandbookPage({super.key});
  @override
  State<StatefulWidget> createState() => _NewcomerHandbookPage();
}

class _NewcomerHandbookPage extends State<NewcomerHandbookPage> {
  final ScrollController scrollController = ScrollController();
  int pageNum = 1;
  int pageSize = 10;
  final list = RxList<ListHandbookItem>([]);
  late String? propId;
  late String propName;

  @override
  void initState() {
    super.initState();
    propId = '26';
    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        _getHandbookList(loadMore: true);
      }
    });
    _getHandbookList();
  }

  @override
  void dispose() {
    super.dispose();
    scrollController.dispose();
  }

  _getHandbookList({loadMore = false}) {
    if (loadMore) {
      pageNum++;
    } else {
      pageNum = 1;
    }

    try {
      http.netFetch<ListHandbookModel>(
        CommonApi.getHandbookPage,
        params: {'pageNum': pageNum, 'pageSize': 20},
        success: (result) {
          List<ListHandbookItem> items = result.data?.list ?? [];
          if (loadMore && items.isEmpty) {
            pageNum--;
          }
          if (!loadMore) {
            list.clear();
          }
          setState(() {
            list.addAll(items);
          });
        },
      );
    } catch (e) {
      if (loadMore) {
        pageNum--;
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    double top = MediaQuery.of(context).padding.top;

    return Scaffold(
      backgroundColor: AppColors.backgroundColorBlack,
      body: Stack(
        children: [
          Positioned(
            top: 0,
            left: 0,
            right: 0,
            child: ImageAssetWidget(
              width: 375.px,
              height: 812.px,
              imageUrl: 'static/images/bg.png',
            ),
          ),

          Column(
            children: [
              Expanded(
                child: Container(
                  padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
                  child: Column(
                    children: [
                      SizedBox(height: top),
                      CustomAppBar2(
                        title: '新人手册',
                        context: context,
                        textColors: AppColors.fontColor,
                      ),
                      list.isNotEmpty
                          ? Expanded(
                            child: Container(
                              padding: EdgeInsets.fromLTRB(15, 10, 15, 0),
                              child: RefreshAndScrollWidget(
                                onRefresh: _handleRefresh,
                                scrollController: scrollController,
                                itemBuilder: (item, index) => _listItem(item),
                                itemCount: list.length,
                                items: list,
                              ),
                            ),
                          )
                          : SizedBox(),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _listItem(item) {
    return GestureDetector(
      onTap: () {
        Get.toNamed(
          '${HandbookDetailPage.path}',
          arguments: {'content': item.content, 'title': item.title},
        );
      },
      child: Container(
        // height: 218.px,
        // constraints: BoxConstraints(maxHeight: 200.px),
        margin: EdgeInsets.only(bottom: 12.px),
        decoration: BoxDecoration(
          color: AppColors.backgroundColorBlackLight,
          borderRadius: BorderRadius.circular(8.px),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            LazyImage(
              imageUrl: '${item.imageUrl}',
              height: 148.px,
              width: 347.px,
              borderRadius: 8.px,
            ),
            Padding(
              padding: EdgeInsets.all(14.px),
              child: Text(
                '${item.title}',
                style: TextStyle(
                  color: AppColors.fontColorGrayE8,
                  fontSize: 14.px,
                  overflow: TextOverflow.ellipsis,
                ),
                textAlign: TextAlign.start,
                maxLines: 2,
              ),
            ),
          ],
        ),
      ),
    );
  }

  Future<void> _handleRefresh() async {
    _getHandbookList();
  }
}
